package core

import (
    "context"
    "os"
    "os/signal"
    "syscall"
    "time"

    log "github.com/sirupsen/logrus"

    "cing.io/cing/internal/apiserver"
    "cing.io/cing/internal/clientcache"
    "cing.io/cing/internal/conductor"
    "cing.io/cing/internal/config"
    "cing.io/cing/internal/k8s"
    "cing.io/cing/internal/storage"
)

func Run() {
    // parse the args
    config.Init()

    // init kubernetes clientset etc.
    ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

    k8s.Init(ctx)

    storage.Init()

    clientcache.Init()

    conductor.Init(ctx)

    apiserver.Init(ctx)

    // listen for interrupts or the Linux SIGTERM signal and cancel our context
    <-ctx.Done()
    log.Info("Received Linux SIGTERM signal, signaling shutdown...")
    log.Info("Shutting down gracefully, press Ctrl+C again to force")
    cancel()

    // exit after 10s
    time.Sleep(10 * time.Second)
    log.Info("System Exit!")
    os.Exit(0)
}
